package com.sanjiahantang.youjiaclient.module.me.require.views.impl;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.PayPsdInputView;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseApplication;
import com.sanjiahantang.youjiaclient.base.BaseBean;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.module.common.utils.CommonModelUtils;
import com.sanjiahantang.youjiaclient.module.evaluate.views.impl.EvaluateServiceActivity;
import com.sanjiahantang.youjiaclient.module.invoice.views.InvoiceView;
import com.sanjiahantang.youjiaclient.module.me.address.beans.AddressBean;
import com.sanjiahantang.youjiaclient.module.me.address.views.AddressView;
import com.sanjiahantang.youjiaclient.module.me.order.beans.RequireOrderBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.Service3PartBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.ServiceListBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.ShopListBean;
import com.sanjiahantang.youjiaclient.module.me.order.presenters.OrderPresenter;
import com.sanjiahantang.youjiaclient.module.me.order.views.OrderView;
import com.sanjiahantang.youjiaclient.module.money.beans.BankCardBean;
import com.sanjiahantang.youjiaclient.module.money.views.impl.MeAddBankListActivity;
import com.sanjiahantang.youjiaclient.utils.KeyBoardUtils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by zy on 2017/10/24.
 */

public class RequireOrderDetailActivity extends BaseMvpActivity<OrderView, OrderPresenter> implements OrderView, View.OnClickListener {

    private View panelCreate, panelPay, panelEvaluate, rlInvoiceInfo;
    private TextView txtState;//状态文字
    private ImageView ivState;//状态图标
    private TextView txtOrderNum;//订单号
    private TextView txtCreateTime;//创建时间
    private ListView goodListView;//列表
    private TextView txtGoodsCount;//数量
    private TextView txtAmount;//价格
    private EditText etConsumerMessage;//留言
    private TextView txtTotal, txtRemain;//合计
    private TextView btnPayForStateUnPay;//应付金额
    private TextView title;
    private TextView txtSubmit, txtJDTitle, txtZFTitle;

    private boolean isRefreshing = false;
    private int type;
    private Service3PartBean.Data.OrderServiceRaDtoListBean responseData;


    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public OrderPresenter initPresenter() {
        return new OrderPresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_order_info);
        //  presenter.bindList($this,listView);
        title = (TextView) findViewById(R.id.title);
        title.setText("订单详情");
        findViewById(R.id.fl_Left).setOnClickListener(this);

        panelCreate = findViewById(R.id.panelCreate);
        panelPay = findViewById(R.id.panelPay);
        panelEvaluate = findViewById(R.id.panelEvaluate);

        txtState = (TextView) findViewById(R.id.txtState);
        ivState = (ImageView) findViewById(R.id.ivState);
        txtOrderNum = (TextView) findViewById(R.id.txtOrderNum);
        txtCreateTime = (TextView) findViewById(R.id.txtCreateTime);
        goodListView = (ListView) findViewById(R.id.goodListView);
        txtGoodsCount = (TextView) findViewById(R.id.txtGoodsCount);
        txtAmount = (TextView) findViewById(R.id.txtAmount);
        etConsumerMessage = (EditText) findViewById(R.id.etConsumerMessage);
        txtTotal = (TextView) findViewById(R.id.txtTotal);
        txtRemain = (TextView) findViewById(R.id.txtRemain);
        btnPayForStateUnPay = (TextView) findViewById(R.id.btnPayForStateUnPay);
        txtSubmit = (TextView) findViewById(R.id.txtSubmit);
        txtSubmit.setOnClickListener(this);
        findViewById(R.id.btnEvaluate).setOnClickListener(this);

        findViewById(R.id.panelInvoice).setVisibility(View.GONE);

        final View llJDTitle = findViewById(R.id.llJDTitle);
        final View llZFTitle = findViewById(R.id.llZFTitle);
        txtZFTitle = (TextView) findViewById(R.id.txtZFTitle);
        txtJDTitle = (TextView) findViewById(R.id.txtJDTitle);
        findViewById(R.id.rlTrade).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (llJDTitle.getVisibility() == View.VISIBLE) {
                    llJDTitle.setVisibility(View.GONE);
                    llZFTitle.setVisibility(View.GONE);
                } else {
                    llJDTitle.setVisibility(View.VISIBLE);
                    llZFTitle.setVisibility(View.VISIBLE);
                }
            }
        });

        final View llInvoiceTitle = findViewById(R.id.llInvoiceTitle);
        final View llInvoiceTaxNo = findViewById(R.id.llInvoiceTaxNo);
        final ImageView ivArrow = (ImageView) findViewById(R.id.ivArrow);
        findViewById(R.id.rlInvoiceInfo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (llInvoiceTitle.getVisibility() == View.VISIBLE) {
                    llInvoiceTitle.setVisibility(View.GONE);
                    llInvoiceTaxNo.setVisibility(View.GONE);
                    ivArrow.setImageResource(R.drawable.i_down_gray);
                } else {
                    llInvoiceTitle.setVisibility(View.VISIBLE);
                    llInvoiceTaxNo.setVisibility(View.VISIBLE);
                    ivArrow.setImageResource(R.drawable.i_up_green);
                }
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData() {
        RequireOrderBean.Data data = (RequireOrderBean.Data) getIntent().getSerializableExtra(KEY_PARAM);
        if (data.getOrderCode() != null) {
            presenter.searchServiceRaForCode($this, data.getOrderCode(), new BaseView.OnLoadListener() {
                @Override
                public void onLoad(Object content) {
                    responseData = (Service3PartBean.Data.OrderServiceRaDtoListBean) content;
                    String statusName = responseData.getStatusName();
                    int resId = R.drawable.i_unpay_head;
                    txtRemain.setVisibility(View.GONE);
                    switch (responseData.getStatusId()) {
                        case OrderView.TYPE_UN_PAY:
                            statusName = "待付款";
                            resId = R.drawable.i_unpay_head;
                            panelPay.setVisibility(View.VISIBLE);
                            panelCreate.setVisibility(View.GONE);
                            panelEvaluate.setVisibility(View.GONE);
                            break;
                        case OrderView.TYPE_PART_PAY:
                            resId = R.drawable.i_unpay_head;
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.VISIBLE);
                            panelEvaluate.setVisibility(View.GONE);

                            txtTotal.setText(String.format("%.2f", responseData.getPayResult()));
                            txtSubmit.setText("去付款");
                            txtRemain.setVisibility(View.VISIBLE);
                            txtRemain.setText(String.format(FORMAT_ORDER_REMAIN,
                                    responseData.getAreadyResult(),
                                    (responseData.getPayMoney() - responseData.getAreadyResult())));
                            break;
                        case OrderView.TYPE_PAYED:
                            resId = R.drawable.i_payed_head;
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.GONE);
                            if (responseData.getServiceType() == 3 && responseData.getServiceRecordList().get(0).getPlanOrderState() == 1) {
                                panelEvaluate.setVisibility(View.VISIBLE);
                            } else {
                                panelEvaluate.setVisibility(View.GONE);
                            }

                            break;
                        case OrderView.TYPE_COMPLETE:
                            statusName = "已完成";
                            resId = R.drawable.i_payed_head;
                            panelEvaluate.setVisibility(View.GONE);
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.GONE);
                            break;
                        case OrderView.TYPE_CLOSE:
                            resId = R.drawable.i_unpay_head;
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.GONE);
                            panelEvaluate.setVisibility(View.GONE);
                            break;
                        case OrderView.TYPE_EVALUATED:
                            statusName = "已完成";
                            resId = R.drawable.i_unpay_head;
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.GONE);
                            panelEvaluate.setVisibility(View.GONE);
                            break;
                        default:
                            resId = R.drawable.i_unpay_head;
                            panelPay.setVisibility(View.GONE);
                            panelCreate.setVisibility(View.GONE);
                            panelEvaluate.setVisibility(View.GONE);
                            break;
                    }
                    txtState.setText(statusName);
                    ivState.setImageResource(resId);
                    txtOrderNum.setText(responseData.getOrderCode());
                    if (responseData.getCreateTime() != null) {
                        txtCreateTime.setText(responseData.getCreateTime().substring(0, "yyyy-MM-dd".length()));
                    }

                    goodListView.setAdapter(new QuickAdapter<Service3PartBean.Data.OrderServiceRaDtoListBean.ServiceRecordListBean>($this, R.layout.item_serviceorderitem, responseData.getServiceRecordList()) {
                        @Override
                        protected void convert(BaseAdapterHelper helper, Service3PartBean.Data.OrderServiceRaDtoListBean.ServiceRecordListBean item) {
                            helper.setText(R.id.sg_name, item.getServiceName());
                            helper.setText(R.id.sg_price, String.format("%.2f", item.getResult()));
                        }
                    });

                    txtAmount.setText(String.format("%.2f", responseData.getPayMoney()));
                    txtGoodsCount.setText(String.valueOf(responseData.getServiceRecordList().size()));
                    btnPayForStateUnPay.setText(String.format("本次应付%.2f元", responseData.getPayResult()));

                    HashMap<String, String> jdOrderInfo = responseData.getJdOrderInfo();
                    ArrayList<HashMap<String, String>> mOrderInfo = responseData.getmOrderInfo();
                    if (jdOrderInfo != null && jdOrderInfo.containsKey("stageOrderId")) {
                        txtJDTitle.setText(jdOrderInfo.get("stageOrderId"));
                    }

                    if (mOrderInfo != null) {
                        String str = "";
                        for (int i = 0, max = mOrderInfo.size(); i < max; i++) {
                            if (mOrderInfo.get(i).containsKey("payOrderId")) {
                                str += mOrderInfo.get(i).get("payOrderId");
                                str += "\n";
                            }
                        }

                        if (!TextUtils.isEmpty(str)) {
                            str = str.substring(0, str.length() - 1);
                        }

                        txtZFTitle.setText(str);

                    }
                }
            });

            btnPayForStateUnPay.setOnClickListener(this);
            findViewById(R.id.btnCancel).setOnClickListener(this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == INTENT_INVOICE && resultCode == RESULT_OK) {
            if (data != null) {
                presenter.setInvoice(data.getStringExtra(KEY_TITLE), data.getStringExtra(InvoiceView.KEY_TAX_NO));
            }
        }
        if (requestCode == INTENT_ADDRESS && resultCode == RESULT_OK) {
            if (data != null) {
                AddressBean.Data address = new AddressBean.Data();
                address.setCustomerName(data.getStringExtra(AddressView.KEY_NAME));
                address.setFullAddress(data.getStringExtra(AddressView.KEY_ADDRESS));
                address.setPhone(data.getStringExtra(AddressView.KEY_PHONE));
                presenter.setAddress(address);
            }
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.txtSubmit:
            case R.id.btnPayForStateUnPay://fukuan
                if (responseData == null) return;
                responseData.setToken(ConfigValue.DATA_KEY);
                responseData.setPhoneTypeId(0);
                presenter.doPayService($this,responseData);
//                final Dialog dialog = new Dialog(this, R.style.Dialog_Fullscreen);
//                final Dialog payPasswordDialog = new Dialog(this, R.style.dialog_common);
//                dialog.setContentView(R.layout.dialog_pay);
//                RelativeLayout WX = (RelativeLayout) dialog.findViewById(R.id.vWX);
//                RelativeLayout ZFB = (RelativeLayout) dialog.findViewById(R.id.vZFB);
//                RelativeLayout BANK = (RelativeLayout) dialog.findViewById(R.id.vBank);
//                RelativeLayout BANK_WEB = (RelativeLayout) dialog.findViewById(R.id.vBankWeb);
//                RelativeLayout POS = (RelativeLayout) dialog.findViewById(R.id.vPos);
//
//
//                final RadioButton rbWX = (RadioButton) dialog.findViewById(R.id.rbWX);
//                final RadioButton rbZFB = (RadioButton) dialog.findViewById(R.id.rbZFB);
//                final RadioButton rbBank = (RadioButton) dialog.findViewById(R.id.rbBank);
//                final RadioButton rbBankWeb = (RadioButton) dialog.findViewById(R.id.rbBankWeb);
//                final RadioButton rbPos = (RadioButton) dialog.findViewById(R.id.rbPos);
//                final TextView btnOK = (TextView) dialog.findViewById(R.id.btnOK);
//                final TextView txtNeedPrice = (TextView) dialog.findViewById(R.id.txtNeedPrice);
//                txtNeedPrice.setText(String.format("%.2f", responseData.getPayResult()));
//                responseData.setToken(ConfigValue.DATA_KEY);
//                responseData.setPhoneTypeId(0);
//                dialog.findViewById(R.id.vWX).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        rbWX.setChecked(true);
//                        rbZFB.setChecked(false);
//                        rbBank.setChecked(false);
//                        rbBankWeb.setChecked(false);
//                        rbPos.setChecked(false);
//                    }
//                });
//
//                dialog.findViewById(R.id.vZFB).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        rbWX.setChecked(false);
//                        rbZFB.setChecked(true);
//                        rbBank.setChecked(false);
//                        rbBankWeb.setChecked(false);
//                        rbPos.setChecked(false);
//                    }
//                });
//
//                dialog.findViewById(R.id.vBank).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        rbWX.setChecked(false);
//                        rbZFB.setChecked(false);
//                        rbBank.setChecked(true);
//                        rbBankWeb.setChecked(false);
//                        rbPos.setChecked(false);
//                    }
//                });
//
//                dialog.findViewById(R.id.vBankWeb).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        rbWX.setChecked(false);
//                        rbZFB.setChecked(false);
//                        rbBank.setChecked(false);
//                        rbBankWeb.setChecked(true);
//                        rbPos.setChecked(false);
//                    }
//                });
//
//                dialog.findViewById(R.id.vPos).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        rbWX.setChecked(false);
//                        rbZFB.setChecked(false);
//                        rbBank.setChecked(false);
//                        rbBankWeb.setChecked(false);
//                        rbPos.setChecked(true);
//                    }
//                });
//                WX.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        rbWX.setChecked(true);
//                        rbZFB.setChecked(false);
//                        rbBank.setChecked(false);
//                        rbBankWeb.setChecked(false);
//                        rbPos.setChecked(false);
//                    }
//                });
//                ZFB.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        rbWX.setChecked(false);
//                        rbZFB.setChecked(true);
//                        rbBank.setChecked(false);
//                        rbBankWeb.setChecked(false);
//                        rbPos.setChecked(false);
//                    }
//                });
//
//                BANK.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        rbWX.setChecked(false);
//                        rbZFB.setChecked(false);
//                        rbBank.setChecked(true);
//                        rbBankWeb.setChecked(false);
//                        rbPos.setChecked(false);
//                    }
//                });
//
//                BANK_WEB.findViewById(R.id.vBankWeb).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        rbWX.setChecked(false);
//                        rbZFB.setChecked(false);
//                        rbBank.setChecked(false);
//                        rbBankWeb.setChecked(true);
//                        rbPos.setChecked(false);
//                    }
//                });
//
//                POS.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        rbWX.setChecked(false);
//                        rbZFB.setChecked(false);
//                        rbBank.setChecked(false);
//                        rbBankWeb.setChecked(false);
//                        rbPos.setChecked(true);
//                    }
//                });
//
//                //支付
//                btnOK.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        dialog.dismiss();
//                        long nowTime = System.currentTimeMillis();
//                        if (Math.abs(ConfigValue.nowTime - nowTime) < DOUBLE_CLICK_TIME_SPACE) {
//                            return;
//                        }
//                        ConfigValue.nowTime = nowTime;
//
//                        if (rbBank.isChecked()) {
//                            if (((TextView) dialog.findViewById(R.id.tvBank)).getText().toString().equals("请先去设置默认银行卡")) {
//                                MeAddBankListActivity.fromSettings($this);
//                                dialog.dismiss();
//                            } else {
//                                responseData.setToken(ConfigValue.DATA_KEY);
//                                responseData.setPhoneTypeId(0);
//                                showProgress();
//                                presenter.getCheckCodeService($this, responseData, new BaseView.OnLoadListener() {
//                                    @Override
//                                    public void onLoad(Object content) {
//                                        if (content != null) {
//                                            responseData.setPayVo((ShopListBean.PayVo) content);
//                                            Utils.showToast($this, "验证码已发送");
//                                            payPasswordDialog.show();
//                                        }
//                                    }
//                                });
//                            }
//                        } else if (rbBankWeb.isChecked()) {
//                            if (((TextView) dialog.findViewById(R.id.tvBank)).getText().toString().equals("请先去设置默认银行卡")) {
//                                MeAddBankListActivity.fromSettings($this);
//                                dialog.dismiss();
//                            } else {
//                                responseData.setToken(ConfigValue.DATA_KEY);
//                                responseData.setPhoneTypeId(0);
//                                responseData.setIsPos(0);
//                                showProgress();
//                                presenter.saveBankPayService($this, responseData, new BaseView.OnLoadListener() {
//                                    @Override
//                                    public void onLoad(Object content) {
//                                    }
//                                });
//                            }
//                        } else if (rbPos.isChecked()) {
//                            responseData.setToken(ConfigValue.DATA_KEY);
//                            responseData.setPhoneTypeId(0);
//                            responseData.setIsPos(1);
//                            showProgress();
//                            presenter.posPay($this, responseData, new BaseView.OnLoadListener() {
//                                @Override
//                                public void onLoad(Object content) {
//                                }
//                            });
//                        } else if (rbWX.isChecked()) {
//                            Utils.showToast($this, "很遗憾，微信暂未接入");
//                        } else if (rbZFB.isChecked()) {
//                            Utils.showToast($this, "很遗憾，支付宝暂未接入");
//                        } else {
//                            Utils.showToast($this, "请选择支付方式");
//                        }
//
//
//                    }
//                });
//
//                View payPasswordView = LayoutInflater.from(this).inflate(R.layout.dialog_pay_password, null);
//                final PayPsdInputView passwordInputView = (PayPsdInputView) payPasswordView.findViewById(R.id.passwordView);
//                payPasswordDialog.setContentView(payPasswordView);
//                passwordInputView.setComparePassword(new PayPsdInputView.onPasswordListener() {
//
//                    @Override
//                    public void onDifference(String oldPsd, String newPsd) {
//
//                    }
//
//                    @Override
//                    public void onEqual(String psd) {
//                    }
//
//                    @Override
//                    public void inputFinished(final String inputPsd) {
//                        responseData.setToken(ConfigValue.DATA_KEY);
//                        responseData.setPhoneTypeId(0);
//                        responseData.setPayPwd(inputPsd);
//                        passwordInputView.cleanPsd();
//                        ShopListBean.PayVo payVoBean = responseData.getPayVo();
//                        if (payVoBean != null) {
//                            payVoBean.setCheckCode(inputPsd);
//                            responseData.setPayVo(payVoBean);
//                        }
//                        KeyBoardUtils.closeKeyboardWithoutFinish($this);
//                        presenter.payServiceOrderPhone($this, 1, responseData);
//                        dialog.dismiss();
//                    }
//                });
//                CommonModelUtils.searchBankCardDefault(this, new BaseView.OnLoadListener() {
//                    @Override
//                    public void onLoad(Object content) {
//                        if (content != null && content instanceof BankCardBean.Item) {
//                            BankCardBean.Item item = (BankCardBean.Item) content;
//                            final TextView tvBank = (TextView) dialog.findViewById(R.id.tvBank);
//                            tvBank.setText(String.format("%s(尾号%s)", item.getBankGenneralName(), Utils.getLast4(item.getBankCardNo())));
//                        }
//                        dialog.show();
////                        else{
////                            MeAddBankListActivity.fromSettings($this);
////                            dialog.dismiss();
////                        }
//                    }
//                });
                break;
            case R.id.btnEvaluate:
                if (responseData == null) return;
                Intent intent = new Intent($this, EvaluateServiceActivity.class);
                ServiceListBean.Data data = new ServiceListBean.Data();
                data.setOrderCode(responseData.getOrderCode());
                intent.putExtra(KEY_PARAM, data);
                startActivity(intent);
                break;
            case R.id.btnCancel:
                if (responseData == null) return;
                responseData.setToken(ConfigValue.DATA_KEY);
                responseData.setPhoneTypeId(0);
                presenter.doCancel($this, responseData);
                break;
        }
    }
}
